// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { checkToken, clearToken } from '@/utils/auth'

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index.vue'),
    redirect: '/user',
    meta: { requiresAuth: true }, // 添加路由元信息
    children: [
      {
        path: '/swiper',
        children: [
          {
            path: '',
            name: 'swiper',
            component: () => import('@/views/swiper/list.vue')
          },
          {
            path: '/swiper/add',
            name: 'SwiperAdd',
            component: () => import('@/views/swiper/edit.vue')
          },
          {
            path: '/swiper/edit/:id',
            name: 'SwiperEdit',
            component: () => import('@/views/swiper/edit.vue')
          },
        ]
      },
      {
        path: '/dealer',
        children: [
          {
            path: '',
            name: 'dealer',
            component: () => import('@/views/dealer/list.vue')
          },
          {
            path: '/dealer/add',
            name: 'DealerAdd',
            component: () => import('@/views/dealer/edit.vue')
          },
          {
            path: '/dealer/edit/:id',
            name: 'DealerEdit',
            component: () => import('@/views/dealer/edit.vue')
          },
        ]
      },
      {
        path: '/store',
        children: [
          {
            path: '',
            name: 'store',
            component: () => import('@/views/store/list.vue')
          },
          {
            path: '/store/add',
            name: 'StoreAdd',
            component: () => import('@/views/store/edit.vue')
          },
          {
            path: '/store/edit/:id',
            name: 'StoreEdit',
            component: () => import('@/views/store/edit.vue')
          },
        ]
      },
      {
        path: '/news',
        children: [
          {
            path: '',
            name: 'news',
            component: () => import('@/views/news/list.vue')
          },
          {
            path: '/news/add',
            name: 'NewsAdd',
            component: () => import('@/views/news/edit.vue')
          },
          {
            path: '/news/edit/:id',
            name: 'NewsEdit',
            component: () => import('@/views/news/edit.vue')
          },
        ]
      },
      {
        path: '/carFilmCategory',
            name: 'CarFilmCategory',
            component: () => import('@/views/carFilmCategory/list.vue')
      },

      {
        path: '/carFilm',
        children: [
          {
            path: '',
            name: 'carFilm',
            component: () => import('@/views/carFilm/list.vue')
          },
          {
            path: '/carFilm/add',
            name: 'CarFilmAdd',
            component: () => import('@/views/carFilm/edit.vue')
          },
          {
            path: '/carFilm/edit/:id',
            name: 'CarFilmEdit',
            component: () => import('@/views/carFilm/edit.vue')
          },
        ]
      },
      {
        path: '/paintFilm',
        children: [
          {
            path: '',
            name: 'paintFilm',
            component: () => import('@/views/paintFilm/list.vue')
          },
          {
            path: '/paintFilm/add',
            name: 'PaintFilmAdd',
            component: () => import('@/views/paintFilm/edit.vue')
          },
          {
            path: '/paintFilm/edit/:id',
            name: 'PaintFilmEdit',
            component: () => import('@/views/paintFilm/edit.vue')
          },
        ]
      },
      {
        path: '/buildingFilm',
        children: [
          {
            path: '',
            name: 'buildingFilm',
            component: () => import('@/views/buildingFilm/list.vue')
          },
          {
            path: '/buildingFilm/add',
            name: 'BuildingFilmAdd',
            component: () => import('@/views/buildingFilm/edit.vue')
          },
          {
            path: '/buildingFilm/edit/:id',
            name: 'BuildingFilmEdit',
            component: () => import('@/views/buildingFilm/edit.vue')
          },
        ]
      },
      {        path: '/user',        name: 'user',        component: () => import('@/views/user/list.vue')      },
      {        path: '/admin',        name: 'admin',        component: () => import('@/views/admin/list.vue')      },
      {
        path: '/company',
        name: 'company',
        component: () => import('@/views/company/list.vue')
      },
      {
        path: '/position',
        name: 'position',
        component: () => import('@/views/position/list.vue')
      },
      {
        path: '/setting/app',
        name: 'settingApp',
        component: () => import('@/views/setting/app.vue')
      },    ]  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/user/login.vue'),
    meta: { requiresAuth: false }
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 不需要认证的路由直接放行
  if (to.matched.some(record => record.meta.requiresAuth === false)) {
    return next()
  }
  // 检查token
  if (checkToken()) {
    // token有效，允许访问
    next()
  } else {
    // token无效，跳转到登录页
    clearToken()
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 保存目标路径以便登录后跳转
    })
  }
})

export default router